<template>
	<view class="select-city-pop">
		<uv-popup ref="popRef" mode="bottom" round="40rpx" closeable>
			<view class="select-city-pop-box">
				<view class="select-city-pop-header">
					<view class="select-city-pop-title">{{isArea ? '请选择经营区域' : '请选择省市'}}</view>
				</view>
				<xh-segment :titles="titles" :selectIndex="selectIndex" @didSelectIndex="didSelectItem"></xh-segment>
				<scroll-view class="scroll-view" :scroll-y="true" >
					<view class="list-box">
						<view class="group" v-for="(item,index) in cityList" :key="index">
							<view class="group-item" v-for="(model,i) in item" :key="model.code" @tap="didSelectCity(model)">
								<view class="group-item-letter">{{ i == 0 ? letterList[index] : ''}}</view>
								<text class="group-item-name">{{model.name}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uv-popup>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import XhSegment from './xh-segment.vue'
	import { useCityStore } from '@/common/stores'
	import { getAllCity } from '@/common/api/api.js'
	const emits = defineEmits(['result'])
	const props = defineProps({
		//是否可以选择区县
		isArea:false
	})
	const cityStore = useCityStore()
	const popRef = ref(null) 
	///城市数据
	const cityList = ref([])
	//字母数组
	const letterList = ref([])
	//选择的城市数据
	const selectCitys = ref([])
	///选择提示组件内容数组
	const titles = ref(['选择省份'])
	///选择提示组件当前下标
	const selectIndex = ref(0)
	//显示
	const open = () => {
		popRef.value.open()
		selectCitys.value = []
		titles.value = ['选择省份']
		selectIndex.value = 0
		getCitysData()
	}
	//关闭
	const close = () => {
		popRef.value.close()
	}
	//获取城市数据
	const getCitysData = async ()=>{
		//请求网络数据
		if(uni.$uv.test.empty(cityStore.cityList)){
			//请求网络数据
			const res = await getAllCity()
			cityStore.cityList = res.result
		}
		formatLetterGroup(cityStore.cityList)
	}
	//城市分组处理
	const formatLetterGroup = (list)=>{
		if(list.length > 0) {
			const count = list.length
			cityList.value = []
			let letters = []
			for (let i = 0;i< count;i++) {
				letters.push(list[i].pyName)
			}
			const resLetters = Array.from(new Set(letters))
			letterList.value = resLetters.sort()
			letterList.value.forEach((letter,i)=>{
				let datas = []
				list.forEach((item,j)=>{
					if (item.pyName == letter){
						datas.push(item)
					}
				})
				cityList.value.push(datas)
			})
		}
		else{
			cityList.value = []
		}
	}
	//选择城市
	const didSelectCity = (item)=>{
		if (selectCitys.value.length == 0) {
			item.cityList.forEach(obj=>{
				if(obj.name == item.name || obj.name.includes('市辖区')){
					obj.code = item.code
					obj.name = item.name
				}
			})
			formatLetterGroup(item.cityList || [])
			selectCitys.value.push(item)
			updateSegmentStatus()
		}
		else if (selectCitys.value.length == 1){
			//选择城市
			if (!props.isArea){
				console.log('不支持')
				//银行卡选择城市，不支持选择区县
				selectCitys.value.push(item)
				emits('result',{provinceName:selectCitys.value[0].name,provinceCode:selectCitys.value[0].code,cityName:selectCitys.value[1].name,cityCode:selectCitys.value[1].code})
				close()
			}
			else{
				//经营区域选择，支持选择区县
				console.log('支持')
				if((item.cityList || []).length > 0) {
					formatLetterGroup(item.cityList || [])
					selectCitys.value.push(item)
					updateSegmentStatus()
				}
				else{
					//没有下一级选择，则确定选择当前城市
					selectCitys.value.push(item)
					emits('result',{provinceName:selectCitys.value[0].name,provinceCode:selectCitys.value[0].code,cityName:selectCitys.value[1].name,cityCode:selectCitys.value[1].code,areaName:'',areaCode:''})
					close()
				}
			}
		}
		else if (selectCitys.value.length == 2){
			//选择区县
			selectCitys.value.push(item)
			emits('result',{provinceName:selectCitys.value[0].name,provinceCode:selectCitys.value[0].code,cityName:selectCitys.value[1].name,cityCode:selectCitys.value[1].code,areaName:selectCitys.value[2].name,areaCode:selectCitys.value[2].code})
			close()
		}
	}
	//更新顶部选择提示组件状态
	const updateSegmentStatus = ()=>{
		if (selectCitys.value.length == 0) {
			titles.value = ['选择省份']
			selectIndex.value = 0
		}
		else if (selectCitys.value.length == 1) {
			const model = selectCitys.value[0]
			titles.value = [model.name,'选择城市']
			selectIndex.value = 1
		}
		else if (selectCitys.value.length == 2) {
			const model1 = selectCitys.value[0]
			const model2 = selectCitys.value[1]
			titles.value = [model1.name,model2.name,'选择区县']
			selectIndex.value = 2
		}
	}
	//选择顶部选择提示组件项
	const didSelectItem = (index)=>{
		if (index >= selectCitys.value.length){
			return
		}
		if (index == 0){
			formatLetterGroup(cityStore.cityList)
			selectCitys.value = []
			updateSegmentStatus()
		}
		else if (index == 1) {
			const model1 = selectCitys.value[0]
			const model2 = selectCitys.value[selectCitys.value.length - 1]
			formatLetterGroup(model1.cityList)
			selectCitys.value.pop()
			updateSegmentStatus()
		}
		
	}
	// 暴露子组件的属性/方法给外界调用
	defineExpose({
		open,
		close
	})
</script>

<style lang="scss" scoped>
.select-city-pop-box{
	background-color: white;
	padding: 30rpx;
	border-top-left-radius: 20rpx;
	border-top-right-radius: 20rpx;
	height: 1000rpx;
	.select-city-pop-header{
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		.select-city-pop-title{
			text-align: center;
			font-size: 36rpx;
			color: $xh-color-3;
		}
		.select-city-pop-header-close{
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			right: 10rpx;
		}
		margin-bottom: 40rpx;
	}
	
	.scroll-view{
		width: 100%;
		height: calc(1000rpx - 200rpx);
		.group-item{
			display: flex;
			align-items: center;
			padding: 20rpx 0; 
			font-size: 28rpx;
			color: $xh-color-3;
			.group-item-letter{
				color: $xh-color-9;
				margin-right: 20rpx;
				width: 30rpx;
			}
			
		}
	}
}
</style>